package com.yawn.zerodefect.service;

import com.yawn.zerodefect.util.Constant;
import org.activiti.engine.HistoryService;
import org.activiti.engine.history.HistoricProcessInstanceQuery;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class CountService {

    @Resource
    private HistoryService historyService;

    private static final String VAR_NAME_PROJECT_ID = "projectId";

    public int rankCount(int rank, String proCode) {
        HistoricProcessInstanceQuery hisProInsQuery = historyService.createHistoricProcessInstanceQuery()
                .processDefinitionKey(Constant.PD_KEY_ZERO_DEFECT)
                .variableValueEquals("rank", rank)
                .variableValueEquals(VAR_NAME_PROJECT_ID, proCode);
        return (int)hisProInsQuery.count();
    }

    public int resolveCount(boolean resolved, String proCode) {
        long finishedCount = historyService.createHistoricProcessInstanceQuery()
                .processDefinitionKey(Constant.PD_KEY_ZERO_DEFECT)
                .variableValueEquals(VAR_NAME_PROJECT_ID, proCode)
                .finished()
                .count();
        long totalCount = historyService.createHistoricProcessInstanceQuery()
                .processDefinitionKey(Constant.PD_KEY_ZERO_DEFECT)
                .variableValueEquals(VAR_NAME_PROJECT_ID, proCode)
                .count();
        long count;
        if (resolved) {
            count = finishedCount;
        } else {
            count = totalCount - finishedCount;
        }
        return (int)count;
    }
}
